package com.example.dormitory_system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitory_system.domain.dto.tsysuser.VisitorAddDTO;
import com.example.dormitory_system.domain.enetiy.Visitor;
import com.example.dormitory_system.domain.vo.tsysuser.VisitorListVO;
import com.example.dormitory_system.service.VisitorService;
import com.example.dormitory_system.mapper.VisitorMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author keenan
* @description 针对表【tb_visitor(访客表)】的数据库操作Service实现
* @createDate 2025-03-26 15:32:07
*/
@Service
public class VisitorServiceImpl extends ServiceImpl<VisitorMapper, Visitor>
    implements VisitorService {
    @Autowired
    private VisitorMapper visitorMapper;

    @Override
    public List<VisitorListVO> sysListVisitor(String username, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if (username == null)
            return visitorMapper.selectList(new LambdaQueryWrapper<Visitor>()).stream()
                    .map(item -> {
                        VisitorListVO visitorListVO = new VisitorListVO();
                        BeanUtil.copyProperties(item, visitorListVO);
                        return visitorListVO;
                    }).collect(Collectors.toList());
        return visitorMapper.selectList(new LambdaQueryWrapper<Visitor>().like(Visitor::getVisitorName, username)).stream()
                .map(item -> {
                    VisitorListVO visitorListVO = new VisitorListVO();
                    BeanUtil.copyProperties(item, visitorListVO);
                    return visitorListVO;
                }).collect(Collectors.toList());
    }

    @Override
    public int addVisitor(VisitorAddDTO visitorAddDTO) {
        return visitorMapper.insert(BeanUtil.copyProperties(visitorAddDTO, Visitor.class));
    }
}